package org.jeecg.modules.system.controller;

import cn.hutool.crypto.digest.DigestUtil;
import com.alibaba.fastjson.JSONObject;
import lombok.extern.slf4j.Slf4j;
import org.jeecg.common.api.vo.Result;
import org.jeecg.common.util.CommonUtils;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Map;

@RestController
@Slf4j
@RequestMapping("/callback")
public class CallbackController {


    @PostMapping("/dky/chargeStatus")
    public Result dkyChargeStatus(@RequestBody JSONObject data) {
        log.info("chargeStatus");
        log.info(data.toJSONString());
        return Result.OK();
    }

    @PostMapping("/dky/chargeOrder")
    public Result dkyChargeOrder(@RequestBody JSONObject data) {
        log.info("chargeOrder");
        log.info(data.toJSONString());
        return Result.OK();
    }

    /**
     * 云快充订单回调
     *
     * @param data
     * @return
     */
    @PostMapping("/ykc/chargingRecord")
    public JSONObject ykcChargingRecord(@RequestBody JSONObject data) {
        JSONObject respObj = new JSONObject();
        log.info("======ykc.chargingRecord=======");
        if (null == data) {
            respObj.put("result", 1);
            respObj.put("data", "无效请求参数");
            return respObj;
        }
        log.info(data.toJSONString());
        try {
            Map<String, String> stringStringMap = CommonUtils.convertToMap(data);
            String s = CommonUtils.sortAndConcatenateParameters(stringStringMap);
            s += "&key=" + CommonUtils.YKC_KEY;
            log.info("签名字符串:{}", s);
            String encode = DigestUtil.md5Hex(s).toUpperCase();
            log.info("生成签名：{}", encode);
        } catch (Exception e) {
        }
        respObj.put("result", 0);
        respObj.put("data", "执行成功");
        return respObj;
    }
}
